import testportal.*

class BootStrap {

    def init = { servletContext ->
		new Region(name: "DevIT", description: "DevIT", active: true).save(failOnError: true)
		new Region(name: "BVT", description: "BVT", active: true).save(failOnError: true)
		new Region(name: "QA", description: "QA", active: true).save(failOnError: true)
		new Environ(name: "BVT1", description: "BVT1", active: true, region: Region.findByName("BVT")).save(failOnError: true)
		new Environ(name: "DevIT1", description: "DevIT1", active: true, region: Region.findByName("DevIT")).save(failOnError: true)
		new Environ(name: "QA1", description: "QA1", active: true, region: Region.findByName("QA")).save(failOnError: true)
		new Environ(name: "QA2", description: "QA2", active: true, region: Region.findByName("QA")).save(failOnError: true)
		new Environ(name: "QA3", description: "QA3", active: true, region: Region.findByName("QA")).save(failOnError: true)
		new Environ(name: "QA4", description: "QA4", active: true, region: Region.findByName("QA")).save(failOnError: true)
		new Environ(name: "QA5", description: "QA5", active: true, region: Region.findByName("QA")).save(failOnError: true)
		
		new App(name: "Transite", description: "Transactional Website", active: true).save(failOnError: true)
		new App(name: "Sassy", description: "Sassy", active: true).save(failOnError: true)
		
		def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
		def adminUser = User.findByUsername('admin') ?: new User(username: 'admin', enabled: true, password: 'admin').save(failOnError: true)
		if(!adminUser.authorities.contains(adminRole)){
			UserRole.create adminUser, adminRole, true
		}
		
    }
    def destroy = {
    }
}
